package com.zhongwang.mms.module.half.service;

import com.zhongwang.mms.gen.entity.HAnnex;
import com.zhongwang.mms.gen.entity.HInventoryMaterielInfo;
import com.zhongwang.mms.gen.entity.User;
import com.zhongwang.mms.module.half.dao.HAnnexDao;
import com.zhongwang.mms.module.half.dao.HInventoryMaterielInfoDao;
import com.zhongwang.mms.module.half.model.LedgerRecordModel;
import com.zhongwang.mms.util.FileUtils;
import com.zhongwang.mms.util.ShiroUtils;
import com.zhongwang.mms.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * @description:台账
 * @author: th
 * @time: 2020/4/15 16:20
 */
@Service
public class HInventoryMaterielInfoService {

    @Autowired
    private HInventoryMaterielInfoDao hInventoryMaterielInfoDao;
    @Autowired
    private HAnnexDao hAnnexDao;
    @Value("${user.upload.path}")
    private String uploadPath;

    //根据库存ID查询所以台账信息
    public List<HInventoryMaterielInfo> selectAllByItyId(Integer ityId) {

        return hInventoryMaterielInfoDao.selectAllByItyId(ityId);
    }

    //新增台账信息
    public int insertLedgerByItyId(LedgerRecordModel ledgerRecordModel) {

        ledgerRecordModel.setImiStatus(1);
        HInventoryMaterielInfo hInventoryMaterielInfo = hInventoryMaterielInfoDao.selectAllByPrimary(ledgerRecordModel.getImiId());
        if(hInventoryMaterielInfo == null){
            return hInventoryMaterielInfoDao.insertLedgerByItyId(ledgerRecordModel);
        }else {
            return hInventoryMaterielInfoDao.updateLedgerByImiId(ledgerRecordModel);
        }

    }

    //查询台账信息
    public List<LedgerRecordModel> selectAllByQuery(LedgerRecordModel ledgerRecordModel) {

        return hInventoryMaterielInfoDao.selectAllByQuery(ledgerRecordModel);
    }

    //根据主键删除台账
    public int deleteLedgerByItyId(Integer imiId) {

        return hInventoryMaterielInfoDao.updateLedgerStatusByImiId(imiId);
    }

    //上传台账文件
    public int uploadFile(MultipartFile[] file, Integer ityId) {

            User user = ShiroUtils.getUser();
            if (null != file && file.length > 0) {
                for (MultipartFile f : file) {
                    String path = uploadPath + "project/" + ityId+ "/";
                    String fileName = FileUtils.upload(f, path);
                    if (StringUtils.isNotNull(fileName)) {
                        String url = "/files/project/" + ityId+ "/" + fileName;
                        if (hAnnexDao.countByUrl(url) > 0) {
                            continue;
                        }
                        HAnnex hAnnex = new HAnnex();
                        hAnnex.setAnxName(fileName);
                        hAnnex.setAnxUrl(url);
                        hAnnex.setAnxFilePath(path + fileName);
                        hAnnex.setAnxItyId(ityId);
                        hAnnex.setAnxStatus(1);
                        hAnnex.setAnxUploadDate(new Date());
                        hAnnex.setAnxUserId(user.getId());
                        hAnnexDao.insert(hAnnex);
                    }
                }
            }
            return 1;
    }

    //删除台账文件
    public int delFile(Integer anxId) {
        return hAnnexDao.delFile(anxId);
    }
}
